//
//  WXApiManager.m
//  WeiYiMei
//
//  Created by 李叶夫 on 2019/5/5.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "WXApiManager.h"
#import "WXPayRequestHandler.h"

@interface WXApiManager ()

@property (nonatomic, copy) NSString *orderId;
@property (nonatomic, copy) NSString *transRecordId;

@end

@implementation WXApiManager

+(instancetype)sharedManager {
    static dispatch_once_t onceToken;
    static WXApiManager *instance;
    dispatch_once(&onceToken, ^{
        instance = [[WXApiManager alloc] init];
    });
    return instance;
}

- (void)wxPayWithOrderId:(NSString *)orderId transRecordId:(NSString *)transRecordId
{
    self.orderId = orderId;
    self.transRecordId = transRecordId;
    [WXPayRequestHandler wxPayRequestWithTransRecordId:self.transRecordId];
}

- (void)onResp:(BaseResp *)resp {
    sleep(1);
    if([resp isKindOfClass:[PayResp class]]){
        //支付返回结果，实际支付结果需要去微信服务器端查询
        NSString *strMsg,*strTitle = [NSString stringWithFormat:@"支付结果"];
        
        switch (resp.errCode) {
            case WXSuccess:
            {
                NSLog(@"支付成功－PaySuccess，retcode = %d", resp.errCode);
                [MBProgressHUD showActivityMessage:@"等待支付结果..."];
                [WXPayRequestHandler wxPayWithTransRecordId:self.transRecordId CallbackSuccess:^(NSURLResponse * _Nonnull response, id  _Nullable responseObj, NSDictionary * _Nullable otherResponse) {
                    [MBProgressHUD hideHUD];
                    NSString *strTitle = @"支付结果";
                    NSString *strMsg = @"支付失败";
                    NSString *orderId = self.orderId;
                    if (responseObj) {
                        if ([responseObj[@"code"] intValue] == 200) {
                            NSNumber *status = responseObj[@"data"][@"status"];
                            if (responseObj[@"data"][@"orderId"]) {
                                orderId = responseObj[@"data"][@"orderId"];
                            }
                            if (status.intValue == 1) {
                                strMsg = @"支付成功";
                            }
                        }
                        else {
                            strMsg = responseObj[@"message"];
                        }
                    }
                    [self alertResult:strTitle message:strMsg orderId:orderId];
                } failure:^(NSURLResponse * _Nullable response, NSError * _Nonnull error) {
                    [MBProgressHUD hideHUD];
                    [self alertResult:@"支付结果查询失败" message:error.userInfo[NSLocalizedDescriptionKey] orderId:self.orderId];
                }];
            }
                
                break;
                
            default:
                strMsg = [NSString stringWithFormat:@"支付失败！retcode = %d, retstr = %@", resp.errCode,resp.errStr];
                NSLog(@"错误，retcode = %d, retstr = %@", resp.errCode,resp.errStr);
                [self alertResult:strTitle message:strMsg orderId:self.orderId];
                break;
        }
        
    }else {
    }
}

- (void)alertResult:(NSString *)title message:(NSString *)message orderId:(NSString *)orderId
{
//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    UIAlertController *vc = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (self.resultHandle) {
            self.resultHandle(orderId);
        }
    }];
    [vc addAction:action];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:vc animated:YES completion:nil];

//    [alert show];
}


@end
